package com.jemi.androidh264streamdemo;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;
import cn.five.stream.manager.CameraStreamingManager;
import cn.five.stream.manager.NetInterface;
import cn.five.stream.manager.StreamingProfile;
import cn.five.stream.surfaceEncoder.filter.FilterManager.FilterType;
import cn.five.stream.surfaceEncoder.widget.CameraSurfaceView;

public class CameraActivity extends Activity implements OnClickListener {
	private static final String TAG = "foxy";
	CameraSurfaceView glSurfaceView = null;
	ImageButton shutterBtn;
	float previewRate = -1f;
	boolean openLight = false;
	private CameraStreamingManager cameraManeger = null;
	private boolean isNet = false;
	int i = 0;
	private TextView tvNum;
	private boolean beauti=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_camera);
		initUI();
		initViewParams();
		shutterBtn.setOnClickListener(new BtnListeners());

	}

	private void initUI() {
		glSurfaceView = (CameraSurfaceView) findViewById(R.id.camera_textureview);
		tvNum = (TextView) this.findViewById(R.id.tv_test);
		shutterBtn = (ImageButton) findViewById(R.id.btn_shutter);
		//glSurfaceView.presetCameraForward(false);
	}

	private void initViewParams() {
		/*LayoutParams params = glSurfaceView.getLayoutParams();
		Point p = DisplayUtil.getScreenMetrics(this);
		params.width = p.x;
		params.height = p.y;
		previewRate = DisplayUtil.getScreenRate(this); // 默认全屏的比例预览
		glSurfaceView.setLayoutParams(params);*/
		//rtmp://gspull.love.tv:1935/live/abc
		StreamingProfile.Stream stream = new StreamingProfile.Stream(
				"rtmp://10.10.10.246:1935/live/testdzs", "");
		StreamingProfile.AudioProfile audioProfile = new StreamingProfile.AudioProfile(
				44100, 64 * 1024);
		StreamingProfile.VideoProfile videoProfile = new StreamingProfile.VideoProfile(
				25, 1024 * 1024, 3, Camera.CameraInfo.CAMERA_FACING_FRONT);

		StreamingProfile profile = new StreamingProfile();
		profile.setStream(stream);
		profile.setAudioProfile(audioProfile);
		profile.setVideoProfile(videoProfile);

		cameraManeger = new CameraStreamingManager(CameraActivity.this,
				glSurfaceView);
		cameraManeger.onPrepare(profile);
		cameraManeger.openDevice();
		// cameraManeger.startStreaming();

		cameraManeger.setNetListener(new NetInterface() {
			@Override
			public void disconnected() {
				Log.i("net", "网络断开");
				if (NetWorkUtils.isNetworkConnected(getApplicationContext())) {
					cameraManeger.reconnectStream();
				}

			}

			@Override
			public void connected() {
				Log.i("net", "网络连接");

			}

			@Override
			public void connectError() {
				Log.i("net", "网络错误");
				if (NetWorkUtils.isNetworkConnected(getApplicationContext())) {
					cameraManeger.reconnectStream();
				}

			}

			@Override
			public void urlError() {
				Log.i("net", "url错误");

			}
		});

		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				cameraManeger.startStreaming();
				
			}
		}).start();
		

		/*LayoutParams p2 = shutterBtn.getLayoutParams();
		p2.width = DisplayUtil.dip2px(this, 80);
		p2.height = DisplayUtil.dip2px(this, 80);
		shutterBtn.setLayoutParams(p2);*/

	}

	int filterIndex = 0;

	private class BtnListeners implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_shutter:
			//	cameraManeger.reconnectStream();
				beauti=!beauti;
				if (beauti) {
					glSurfaceView.changeFilter(FilterType.Beauty);
				}else{
					glSurfaceView.changeFilter(FilterType.Normal);
				}
//				cameraManeger.switchCamera();
				//cameraManeger.startStreaming();
				break;
			default:
				break;
			}
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		cameraManeger.onResume();
	}

	@Override
	protected void onPause() {
		cameraManeger.onPause();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		cameraManeger.onDestory();
		cameraManeger = null;
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {

	}

}
